package com.ua.open.authentication.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.ua.open.common.persistence.CommonMapper;

/**
 * 开放（公共）页面控制器
 * @author Winsy
 *
 */
@Controller
@RequestMapping("/open/*")
public class OpenController {
	
	@Autowired
	private Producer captchaProducer;
	
	@Autowired
	private CommonMapper publicMapper;
	
	/**
	 * 公共首页
	 * @return
	 */
	@RequestMapping(value="index.html")
	public ModelAndView index(HttpServletRequest req) {
		ModelAndView mnv = new ModelAndView();
		String requestedWith = req.getHeader("X-Requested-With");
		boolean isAjax = requestedWith != null ? "XMLHttpRequest".equals(requestedWith) : false;
		if(isAjax){
			//ajax请求时，发生页面超时
			mnv.setViewName("open/ajaxSessionTimeout");
		}else{
			mnv.setViewName("open/index");
		}
		return mnv;
	}
	/**
	 * 登陆页面
	 * @return
	 */
	@RequestMapping(value="login.html")
	public ModelAndView login() {
		ModelAndView mnv = new ModelAndView();
		mnv.setViewName("open/authentication/login");
		return mnv;
	}
	
	/**
	 * 注销页面
	 * @return
	 */
	@RequestMapping(value="logout.html")
	public ModelAndView logout() {
		ModelAndView mnv = new ModelAndView();
		mnv.setViewName("open/index");
		return mnv;
	}
	
	/**
	 * 产生验证码图片，验证码保存到session中
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/validateImage")
	public String image(HttpServletRequest request, HttpServletResponse response)throws IOException {
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control","no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		response.setContentType("image/jpeg");
		String capText = captchaProducer.createText();
		request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,capText);
		BufferedImage bi = captchaProducer.createImage(capText);
		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(bi, "jpg", out);
		try {
			out.flush();
		} finally {
			out.close();
		}
		return null;
	}
	
	/**
	 * 没有权限页面
	 * @return
	 */
	@RequestMapping(value="accessdenied.html")
	public ModelAndView accessdenied() {
		ModelAndView mnv = new ModelAndView();
		mnv.setViewName("open/common/403");
		return mnv;
	}
	
	/**
	 * 发生运行时异常
	 * @return
	 */
	@RequestMapping(value="rtError.html")
	public ModelAndView runtimeException() {
		ModelAndView mnv = new ModelAndView();
		mnv.setViewName("open/common/rtError");
		return mnv;
	}
	
	/**
	 * 页面找不到异常（HTTP 404）
	 * @return
	 */
	@RequestMapping(value="404.html")
	public ModelAndView notFoundException() {
		ModelAndView mnv = new ModelAndView();
		mnv.setViewName("open/common/404");
		return mnv;
	}
	
	/**
	 * 发生内部服务器错误（HTTP 500）
	 * @return
	 */
	@RequestMapping(value="500.html")
	public ModelAndView internalServerException() {
		ModelAndView mnv = new ModelAndView();
		mnv.setViewName("open/common/500");
		return mnv;
	}
	
	/**
	 * 发生业务执行异常
	 * @return
	 */
	@RequestMapping(value="bizError.html")
	public ModelAndView bizErrorException() {
		ModelAndView mnv = new ModelAndView();
		mnv.setViewName("open/common/bizError");
		return mnv;
	}

	/**
	 * 保存时区偏差值(小时)
	 * @param request
	 */
	@RequestMapping(value="timeZone.html")
	public void saveTimeZone(HttpServletRequest request){
		int timeZone = Integer.parseInt(request.getParameter("timeZone"));
		HttpSession session = (HttpSession) request.getSession();
		session.setAttribute("timeZone", timeZone);
	}
	
	/**
	 * 关于我们页面
	 * @param request
	 */
	@RequestMapping(value="aboutUs.html")
	public String aboutUs(){
		return "open/aboutUs";
	}
	
	/**
	 * 联系我们页面
	 * @param request
	 */
	@RequestMapping(value="contactUs.html")
	public String contactUs(){
		return "open/contactUs";
	}
	
	/**
	 * 隐私策略
	 * @param request
	 */
	@RequestMapping(value="secret.html")
	public String secret(){
		return "open/secret";
	}
	
	/**
	 * 使用政策和访客协议|
	 * @param request
	 */
	@RequestMapping(value="agreement.html")
	public String agreement(){
		return "open/agreement";
	}
}
